home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 April: Mac OS SDK / Dev.CD Apr 96 SDK / Dev.CD Apr 96 SDK1.toast / Development Kits (Disc 1) / OpenDoc Development Framework / ODFDev / ODF / Examples / Draw / Sources / DrawSel.cpp < prev    next >
Encoding:
Text File  |  1995-11-08  |  32.0 KB  |  1,113 lines  |  [TEXT/MPS ]

  1. //========================================================================================
  2. //
  3. //    File:                DrawSel.cpp
  4. //    Release Version:    $ 1.0d11 $
  5. //
  6. //    Author:                Henri Lamiraux
  7. //
  8. //    Copyright:    © 1993, 1995 by Apple Computer, Inc., all rights reserved.
  9. //
  10. //========================================================================================
  11.  
  12. #include "ODFDraw.hpp"
  13.  
  14. #ifndef DRAWSEL_H
  15. #include "DrawSel.h"
  16. #endif
  17.  
  18. #ifndef DRAWFRM_H
  19. #include "DrawFrm.h"
  20. #endif
  21.  
  22. #ifndef DRAWPART_H
  23. #include "DrawPart.h"
  24. #endif
  25.  
  26. #ifndef BASESHP_H
  27. #include "BaseShp.h"
  28. #endif
  29.  
  30. #ifndef BOUNDSHP_H
  31. #include "BoundShp.h"
  32. #endif
  33.  
  34. #ifndef LINESHP_H
  35. #include "LineShp.h"
  36. #endif
  37.  
  38. #ifndef OVALSHP_H
  39. #include "OvalShp.h"
  40. #endif
  41.  
  42. #ifndef RECTSHP_H
  43. #include "RectShp.h"
  44. #endif
  45.  
  46. #ifndef RRECTSHP_H
  47. #include "RRectShp.h"
  48. #endif
  49.  
  50. #ifndef TEXTSHP_H
  51. #include "TextShp.h"
  52. #endif
  53.  
  54. #ifndef DRAWCLIP_H
  55. #include "DrawClip.h"
  56. #endif
  57.  
  58. #ifndef DRAWPRXY_H
  59. #include "DrawPrxy.h"
  60. #endif
  61.  
  62. #ifndef UTILS_H
  63. #include "Utils.h"
  64. #endif
  65.  
  66. #ifndef SHPTRAKR_H
  67. #include "ShpTrakr.h"
  68. #endif
  69.  
  70. #ifndef DRAWCMDS_H
  71. #include "DrawCmds.h"
  72. #endif
  73.  
  74. #ifndef DRAWLINK_H
  75. #include "DrawLink.h"
  76. #endif
  77.  
  78. #ifndef DRWPRMSE_H
  79. #include "DrwPrmse.h"
  80. #endif
  81.  
  82. // ----- Part Layer -----
  83.  
  84. #ifndef FWITERS_H
  85. #include "FWIters.h"
  86. #endif
  87.  
  88. #ifndef FWUTIL_H
  89. #include "FWUtil.h"
  90. #endif
  91.  
  92. #ifndef FWPRESEN_H
  93. #include "FWPresen.h"
  94. #endif
  95.  
  96. // ----- OS Layer -----
  97.  
  98. #ifndef FWORDCOL_H
  99. #include "FWOrdCol.h"
  100. #endif
  101.  
  102. #ifndef FWSUSINK_H
  103. #include "FWSUSink.h"
  104. #endif
  105.  
  106. #ifndef FWODGEOM_H
  107. #include "FWODGeom.h"
  108. #endif
  109.  
  110. #ifndef FWEVENT_H
  111. #include "FWEvent.h"
  112. #endif
  113.  
  114. // ----- Foundation Includes -----
  115.  
  116. #ifndef FWSTREAM_H
  117. #include "FWStream.h"
  118. #endif
  119.  
  120. // ----- OpenDoc Includes -----
  121.  
  122. #ifndef SOM_ODShape_xh
  123. #include <Shape.xh>
  124. #endif
  125.  
  126. #ifndef SOM_ODStorageUnit_xh
  127. #include <StorageU.xh>
  128. #endif
  129.  
  130. #ifndef SOM_Module_OpenDoc_StdProps_defined
  131. #include <StdProps.xh>
  132. #endif
  133.  
  134. #ifndef SOM_ODSession_xh
  135. #include <ODSessn.xh>
  136. #endif
  137.  
  138. //========================================================================================
  139. //    Runtime Informations
  140. //========================================================================================
  141.  
  142. #ifdef FW_BUILD_MAC
  143. #pragma segment odfdraw2
  144. #endif
  145.  
  146. //========================================================================================
  147. //    class CDrawSelection
  148. //========================================================================================
  149.  
  150. //----------------------------------------------------------------------------------------
  151. //    CDrawSelection::CDrawSelection
  152. //----------------------------------------------------------------------------------------
  153.  
  154. CDrawSelection::CDrawSelection(Environment* ev, CDrawPart* drawPart):
  155.     FW_CEmbeddingSelection(ev, TRUE, TRUE),
  156.     fDrawPart(drawPart),
  157.     fUpdateShape(NULL),
  158.     fProxyShapeCount(0),
  159.     fFrozenCount(0),
  160.     fCount(0),
  161.     fCollection(NULL),
  162.     fWorkingHandle(FW_kZeroRect, FW_kFill)
  163. {
  164.     fCollection = new FW_CPrivOrderedCollection;
  165.     fWorkingHandle.SetInk(FW_kInvertInk);
  166. }
  167.  
  168. //----------------------------------------------------------------------------------------
  169. //    CDrawSelection::~CDrawSelection
  170. //----------------------------------------------------------------------------------------
  171.  
  172. CDrawSelection::~CDrawSelection()
  173. {    
  174.     if (fUpdateShape)
  175.         fUpdateShape->Release(somGetGlobalEnvironment());
  176.         
  177.     delete fCollection;
  178. }
  179.  
  180. //----------------------------------------------------------------------------------------
  181. //    CDrawSelection::WhichHandle
  182. //----------------------------------------------------------------------------------------
  183.  
  184. CBaseShape* CDrawSelection::WhichHandle(Environment* ev, FW_CGraphicContext& gc, const FW_CPoint& mouse, short& whichHandle) const
  185. {
  186.     whichHandle = 0;
  187.     
  188.     if (fCount != 0)
  189.     {
  190.         FW_COrderedCollectionIterator ite(fCollection);
  191.         for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  192.         {
  193.             whichHandle = shape->WhichHandle(gc, mouse);
  194.             if (whichHandle != 0)
  195.                 return shape;
  196.         }
  197.     }
  198.     
  199.     return NULL;
  200. }
  201.  
  202. //----------------------------------------------------------------------------------------
  203. //    CDrawSelection::RenderSelectionHandles
  204. //----------------------------------------------------------------------------------------
  205.  
  206. void CDrawSelection::RenderSelectionHandles(Environment* ev, FW_CGraphicContext& gc)
  207. {
  208.     FW_COrderedCollectionIterator ite(fCollection);
  209.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  210.     {
  211.         shape->RenderHandles(gc);    
  212.     }
  213. }
  214.  
  215. //----------------------------------------------------------------------------------------
  216. //    CDrawSelection::RenderAllHandles
  217. //----------------------------------------------------------------------------------------
  218.  
  219. void CDrawSelection::RenderAllHandles(Environment* ev, FW_CFrame* frame)
  220. {
  221.     if (fCount != 0)
  222.     {        
  223.         FW_CFrameFacetIterator ite(ev, frame);
  224.         for (ODFacet* facet = (ODFacet*)ite.First(ev); ite.IsNotComplete(ev); facet = (ODFacet*)ite.Next(ev))
  225.         {
  226.             FW_CViewContext vc(ev, frame->GetContentView(ev), facet);
  227.             RenderSelectionHandles(ev, vc);
  228.         }
  229.     }
  230. }
  231.  
  232. //----------------------------------------------------------------------------------------
  233. //    CDrawSelection::RenderHandles
  234. //----------------------------------------------------------------------------------------
  235.  
  236. void CDrawSelection::RenderHandles(Environment* ev, CBaseShape* shape)
  237. {
  238.     FW_CPresentationFrameIterator ite(GetPresentation(ev));
  239.     for (FW_CFrame* frame = ite.First(); ite.IsNotComplete(); frame = ite.Next())
  240.     {
  241.         if (frame->IsActive(ev))
  242.         {
  243.             FW_CFrameFacetIterator i(ev, frame);            
  244.             for (ODFacet* facet = i.First(ev); i.IsNotComplete(ev); facet = i.Next(ev))
  245.             {
  246.                 FW_CViewContext vc(ev, frame->GetContentView(ev), facet);
  247.                 shape->RenderHandles(vc);
  248.             }
  249.         }
  250.     }
  251. }
  252.  
  253. //----------------------------------------------------------------------------------------
  254. //    CDrawSelection::CloseSelection
  255. //----------------------------------------------------------------------------------------
  256.  
  257. void CDrawSelection::CloseSelection(Environment* ev)
  258. {
  259.     while (fCollection->Count() != 0)
  260.     {
  261.         CBaseShape *shape = (CBaseShape*)fCollection->First();
  262.         DoRemove(ev, shape);
  263.         RenderHandles(ev, shape);        // turn off
  264.     }
  265.  
  266.     CalcCache(ev);
  267. }
  268.  
  269. //----------------------------------------------------------------------------------------
  270. //    CDrawSelection::AddToSelection
  271. //----------------------------------------------------------------------------------------
  272.  
  273. void CDrawSelection::AddToSelection(Environment* ev, CBaseShape* theShape, FW_Boolean renderHandles)
  274. {                
  275.     if (theShape != NULL)
  276.     {
  277.         DoAdd(ev, theShape);
  278.         if (renderHandles)
  279.             RenderHandles(ev, theShape);    // Turn on    
  280.     }        
  281.             
  282.     CalcCache(ev);
  283. }
  284.  
  285. //----------------------------------------------------------------------------------------
  286. //    CDrawSelection::RemoveFromSelection
  287. //----------------------------------------------------------------------------------------
  288.  
  289. void CDrawSelection::RemoveFromSelection(Environment* ev, CBaseShape* shape, FW_Boolean renderHandles)
  290. {                
  291.     if (shape != NULL)
  292.     {
  293.         DoRemove(ev, shape);
  294.         if (renderHandles)
  295.             RenderHandles(ev, shape);        // Turn Off
  296.     }    
  297.     
  298.     CalcCache(ev);
  299. }
  300.  
  301. //----------------------------------------------------------------------------------------
  302. //    CDrawSelection::DoAdd
  303. //----------------------------------------------------------------------------------------
  304.  
  305. void CDrawSelection::DoAdd(Environment* ev, CBaseShape *shape)
  306. {
  307.     shape->SelectShape(ev, TRUE);
  308.     fCollection->AddLast(shape);
  309.     fCount++;
  310.     if (shape->GetShapeType() == kProxyShape)
  311.         fProxyShapeCount++;
  312.     if (shape->IsFrozen())
  313.         fFrozenCount++;
  314. }
  315.  
  316. //----------------------------------------------------------------------------------------
  317. //    CDrawSelection::DoRemove
  318. //----------------------------------------------------------------------------------------
  319.  
  320. void CDrawSelection::DoRemove(Environment* ev, CBaseShape *shape)
  321. {
  322.     shape->SelectShape(ev, FALSE);
  323.     fCollection->Remove(shape);
  324.     fCount--;
  325.     if (shape->GetShapeType() == kProxyShape)
  326.         fProxyShapeCount--;
  327.     if (shape->IsFrozen())
  328.         fFrozenCount--;
  329. }
  330.  
  331. //----------------------------------------------------------------------------------------
  332. //    CDrawSelection::ClearSelection
  333. //----------------------------------------------------------------------------------------
  334.  
  335. FW_Boolean CDrawSelection::ClearSelection(Environment* ev)
  336. {
  337.     CBaseShape *shape;
  338.     while (fCollection->Count() != 0)
  339.     {
  340.         shape = (CBaseShape*)fCollection->First();
  341.         shape->CheckPromise(ev, fDrawPart);
  342.         
  343.         DoRemove(ev, shape);                // Remove from Selection list
  344.         fDrawPart->RemoveShape(ev, shape);    // Remove from shape list
  345.     }
  346.     
  347.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  348.     facetClipper.Clip(ev, GetPresentation(ev), fUpdateShape);
  349.     GetPresentation(ev)->Invalidate(ev, fUpdateShape);
  350.     
  351.     CalcCache(ev);
  352.     
  353.     return TRUE;
  354. }
  355.  
  356. //----------------------------------------------------------------------------------------
  357. //    CDrawSelection::SelectAll
  358. //----------------------------------------------------------------------------------------
  359.  
  360. void CDrawSelection::SelectAll(Environment* ev)
  361. {    
  362.     FW_CFrame* activeFrame = fDrawPart->GetActiveFrame(ev);
  363.     
  364.     RenderAllHandles(ev, activeFrame);
  365.  
  366.     FW_COrderedCollectionIterator ite(fDrawPart->GetShapeList());
  367.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  368.     {
  369.         if (!shape->IsSelectedShape())
  370.             DoAdd(ev, shape);
  371.     }
  372.     
  373.     RenderAllHandles(ev, activeFrame);
  374.  
  375.     CalcCache(ev);
  376.     
  377.     fDrawPart->SetTool(ev, kSelectTool);
  378. }
  379.  
  380. //----------------------------------------------------------------------------------------
  381. //    CDrawSelection::IsEmpty
  382. //----------------------------------------------------------------------------------------
  383.  
  384. FW_Boolean CDrawSelection::IsEmpty(Environment* ev) const
  385. {    
  386.     return fCount == 0;
  387. }
  388.  
  389. //----------------------------------------------------------------------------------------
  390. //    CDrawSelection::IsSelectionOnlyOneProxy
  391. //----------------------------------------------------------------------------------------
  392.  
  393. FW_MProxy* CDrawSelection::IsSelectionOnlyOneProxy(Environment* ev) const
  394. {
  395.     if (fCount == 1 && fProxyShapeCount == 1)
  396.     {
  397.         return (CProxyShape*)fCollection->First();
  398.     }
  399.     
  400.     return NULL;
  401. }
  402.  
  403. //----------------------------------------------------------------------------------------
  404. //    CDrawSelection::DoExternalizeSelection
  405. //----------------------------------------------------------------------------------------
  406. void CDrawSelection::DoExternalizeSelection(Environment* ev, 
  407.                                             ODStorageUnit* destinationSU, 
  408.                                             FW_CCloneInfo* cloneInfo)
  409. {
  410.     // ----- Create an archive for our shapes -----
  411.     FW_CStorageUnitSink sink(destinationSU, kODPropContents, fDrawPart->GetPartKind(ev));
  412.     CDrawWritableStream archive(ev, &sink, cloneInfo);
  413.     
  414.     // ----- Write number of shapes -----
  415.     unsigned long count = fCollection->Count();
  416.     archive << count;
  417.     
  418.     // ----- Write top left offset -----
  419.     FW_CFixed offset = FW_IntToFixed(0);
  420.     archive << offset;
  421.     archive << offset;
  422.  
  423.     // ----- Write shapes -----
  424.     FW_COrderedCollectionIterator ite(fCollection);
  425.     for (CBaseShape* theShape = (CBaseShape*)ite.First(); ite.IsNotComplete(); theShape = (CBaseShape*)ite.Next())
  426.     {
  427.         FW_WRITE_DYNAMIC_OBJECT(archive, theShape, CBaseShape);
  428.     }    
  429. }
  430.  
  431. //----------------------------------------------------------------------------------------
  432. //    CDrawSelection::DoInternalizeSelection
  433. //----------------------------------------------------------------------------------------
  434.  
  435. FW_Boolean CDrawSelection::DoInternalizeSelection(Environment* ev, 
  436.                                                     ODStorageUnit* sourceSU, 
  437.                                                     FW_CCloneInfo* cloneInfo)
  438. {
  439.     if (sourceSU->Exists(ev, kODPropContents, fDrawPart->GetPartKind(ev), 0))
  440.     {
  441.         // ----- Close First the current selection -----
  442.         CloseSelection(ev);
  443.         
  444.         // ----- [HLX] force fulfill promises Otherwise bug in locks
  445.         sourceSU->Focus(ev, kODPropContents, kODPosUndefined, fDrawPart->GetPartKind(ev), 0, kODPosUndefined);
  446.         sourceSU->GetSize(ev);
  447.         
  448.         // ----- Create an archive object for the content property -----
  449.         FW_CStorageUnitSink sink(sourceSU, kODPropContents, fDrawPart->GetPartKind(ev));
  450.         CDrawReadableStream archive(ev, fDrawPart, &sink, cloneInfo);
  451.  
  452.         // ----- Read number of shapes -----
  453.         unsigned long count;
  454.         archive >> count;
  455.     
  456.         // ----- Read top left offset -----
  457.         FW_CFixed xTopLeft, yTopLeft;
  458.         archive >> xTopLeft;
  459.         archive >> yTopLeft;
  460.         
  461.         // ----- Then read in the new selection -----
  462.         for (unsigned short i = 0; i<count; i++)
  463.         {
  464.             CBaseShape* theShape = NULL;
  465.             FW_READ_DYNAMIC_OBJECT(archive, &theShape, CBaseShape);    
  466.             FW_ASSERT(theShape);
  467.                         
  468.             // ----- Add it at the right place -----
  469.             fDrawPart->AddShape(ev, theShape);
  470.             
  471.             this->DoAdd(ev, theShape);        // Add to the selection list
  472.         }
  473.  
  474.         CalcCache(ev);
  475.  
  476.         return TRUE;
  477.     }
  478.     
  479.     return FALSE;
  480. }
  481.  
  482. //----------------------------------------------------------------------------------------
  483. //    CDrawSelection::InsertNewPart
  484. //----------------------------------------------------------------------------------------
  485.  
  486. FW_Boolean CDrawSelection::InsertNewPart(Environment* ev, FW_CFrame* scopeFrame)
  487. {
  488.     FW_Boolean result = FW_CEmbeddingSelection::InsertNewPart(ev, scopeFrame);
  489.     
  490.     if (result)
  491.         AdjustSelectionAfterPaste(ev, scopeFrame);
  492.     
  493.     return result;
  494. }
  495.  
  496. //----------------------------------------------------------------------------------------
  497. //    CDrawSelection::AdjustSelectionAfterPaste
  498. //----------------------------------------------------------------------------------------
  499.  
  500. FW_CPoint CDrawSelection::AdjustSelectionAfterPaste(Environment* ev, FW_CFrame* scopeFrame)
  501. {
  502.     FW_ASSERT(scopeFrame);
  503.     
  504.     fDrawPart->SetTool(ev, kSelectTool);
  505.         
  506.     FW_CRect frameBounds = scopeFrame->GetContentView(ev)->GetBounds(ev);
  507.     frameBounds.Place(FW_kFixed0, FW_kFixed0);
  508.     scopeFrame->GetContentView(ev)->ViewToViewContent(ev, frameBounds);
  509.     
  510.     FW_CRect box(fDragRect);
  511.     box.PlaceInCenter(frameBounds);
  512.             
  513.     FW_CPoint result(box.left - fDragRect.left, box.top - fDragRect.top);
  514.     OffsetSelection(ev, result.x, result.y);
  515.     
  516.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  517.     facetClipper.Clip(ev, GetPresentation(ev), fUpdateShape);    
  518.     GetPresentation(ev)->Invalidate(ev, fUpdateShape);
  519.  
  520.     return result;
  521. }
  522.  
  523. //----------------------------------------------------------------------------------------
  524. //    CDrawSelection::Resize
  525. //----------------------------------------------------------------------------------------
  526.  
  527. FW_Boolean CDrawSelection::Resize(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  528. {    
  529.     if (fClickedHandle == 0)
  530.         return FALSE;
  531.         
  532.     if (!this->IsOKtoEdit(ev))    // can't resize read only part
  533.     {
  534.         FW_Beep();
  535.         return TRUE;
  536.     }
  537.                 
  538.     if (!theMouseEvent.WaitUntilMouseMoved(ev))
  539.         return TRUE;
  540.     
  541.     ODFacet* facet = theMouseEvent.GetFacet(ev);
  542.     FW_CFrame *frame = FW_CFrame::ODtoFWFrame(ev, facet->GetFrame(ev));
  543.  
  544.     CBaseShape* anchorShape = GetAnchorShape();
  545.     
  546.     FW_PInk resizeInk(FW_kRGBBlack, FW_kRGBWhite, FW_kXOr);
  547.     FW_PStyle resizeStyle(anchorShape->GetResizeStyle());
  548.     
  549.     CResizeTracker resizeTracker(ev, frame->GetContentView(ev), facet, anchorShape, fClickedHandle, resizeInk, resizeStyle, TRUE);
  550.     if (resizeTracker.Track(ev, theMouseEvent))
  551.     {
  552.         GetPresentation(ev)->Invalidate(ev, fUpdateShape);
  553.  
  554.         FW_CPoint lastLocation;
  555.         resizeTracker.GetLastLocation(lastLocation);
  556.         FW_CRect srcRect, dstRect;
  557.         anchorShape->GetMapRects(fClickedHandle, lastLocation, srcRect, dstRect);
  558.  
  559.         CResizeShapeCommand* cmd = FW_NEW(CResizeShapeCommand,
  560.                                             (ev, fDrawPart, frame, this,
  561.                                              srcRect, dstRect));
  562.         fDrawPart->ExecuteCommand(ev, cmd);
  563.     }
  564.     else
  565.     {
  566.         FW_CViewContext vc(ev, frame->GetContentView(ev), facet);
  567.  
  568.         anchorShape->CalcHandle(fClickedHandle, &fWorkingHandle);
  569.         fWorkingHandle.Render(vc);    // redraw the handle
  570.     }
  571.     
  572.     return TRUE;
  573. }
  574.  
  575. //----------------------------------------------------------------------------------------
  576. //    CDrawSelection::CalcCache
  577. //----------------------------------------------------------------------------------------
  578.  
  579. void CDrawSelection::CalcCache(Environment* ev)
  580. {
  581.     fDragRect.Clear();
  582.     
  583.     if (fUpdateShape == NULL)
  584.         fUpdateShape = ::FW_NewODShape(ev);
  585.     
  586.     if (fCount == 0)
  587.         return;
  588.     
  589.     FW_CAcquiredODShape aqTempShape = ::FW_NewODShape(ev);
  590.     FW_CRect tempRect;
  591.  
  592.     FW_Boolean first = TRUE;
  593.     FW_COrderedCollectionIterator ite(fCollection);
  594.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  595.     {
  596.         shape->GetDragRect(tempRect);
  597.         shape->GetUpdateBox(ev, aqTempShape);
  598.         if (first)
  599.         {
  600.             fDragRect = tempRect;
  601.             fUpdateShape->CopyFrom(ev, aqTempShape);
  602.         }
  603.         else
  604.         {
  605.             fDragRect |= tempRect;
  606.             fUpdateShape->Union(ev, aqTempShape);
  607.         }
  608.                         
  609.         first = FALSE;
  610.     }
  611.     
  612. }
  613.  
  614. //----------------------------------------------------------------------------------------
  615. //    CDrawSelection::CreateSelectionShape
  616. //----------------------------------------------------------------------------------------
  617.  
  618. ODShape* CDrawSelection::CreateSelectionShape(Environment* ev, ODFacet* facet, FW_CFrame* frame) const
  619. {
  620. FW_UNUSED(frame);
  621. FW_UNUSED(facet);
  622.  
  623.     ODShape* shape = ::FW_NewODShape(ev, fDragRect);
  624.     return shape;
  625. }
  626.  
  627. //----------------------------------------------------------------------------------------
  628. //    CDrawSelection::CreateSelectionShape
  629. //----------------------------------------------------------------------------------------
  630.  
  631. ODShape* CDrawSelection::CreateSelectionOutline(Environment* ev, ODFacet* facet, FW_CFrame* frame) const
  632. {
  633.     ODShape* outline = GetAnchorShape()->CreateShapeOutline(ev);
  634.  
  635.     if (fCount > 1)
  636.     {
  637.         FW_CAcquiredODShape shapeOutline = FW_CEmbeddingSelection::CreateSelectionOutline(ev, facet, frame);
  638.         outline->Union(ev, shapeOutline);
  639.     }
  640.  
  641.     return outline;
  642. }
  643.  
  644. //----------------------------------------------------------------------------------------
  645. //    CDrawSelection::MapSelection
  646. //----------------------------------------------------------------------------------------
  647.  
  648. void CDrawSelection::MapSelection(Environment* ev, const FW_CRect& srcRect, const FW_CRect& dstRect)
  649. {
  650.     FW_CAcquiredODShape aqUnionShape(fUpdateShape->Copy(ev));
  651.     
  652.     FW_COrderedCollectionIterator ite(fCollection);
  653.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  654.     {
  655.         shape->MapShape(ev, fDrawPart, srcRect, dstRect);
  656.     }
  657.     
  658.     CalcCache(ev);
  659.  
  660.     aqUnionShape->Union(ev, fUpdateShape);
  661.     
  662.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  663.     facetClipper.Clip(ev, GetPresentation(ev), aqUnionShape);
  664. }
  665.  
  666. //----------------------------------------------------------------------------------------
  667. //    CDrawSelection::CreateSelectionFrameShape
  668. //----------------------------------------------------------------------------------------
  669.  
  670. ODShape* CDrawSelection::CreateSelectionFrameShape(Environment* ev) const
  671. {
  672.     
  673.     FW_CRect selectionRect(fDragRect);
  674.     selectionRect.Place(FW_kZeroPoint);
  675.     
  676.     ODShape* selectionShape = ::FW_NewODShape(ev, selectionRect);
  677.     return selectionShape;
  678. }
  679.  
  680. //----------------------------------------------------------------------------------------
  681. //    CDrawSelection::OffsetSelection
  682. //----------------------------------------------------------------------------------------
  683.  
  684. void CDrawSelection::OffsetSelection(Environment* ev, FW_CFixed xDelta, FW_CFixed yDelta)
  685. {
  686.     FW_CAcquiredODShape aqUnionShape(fUpdateShape->Copy(ev));
  687.     
  688.     FW_COrderedCollectionIterator ite(fCollection);
  689.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  690.     {
  691.         shape->OffsetShape(ev, xDelta, yDelta);
  692.     }
  693.  
  694.     CalcCache(ev);
  695.  
  696.     aqUnionShape->Union(ev, fUpdateShape);
  697.     
  698.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  699.     facetClipper.Clip(ev, GetPresentation(ev), aqUnionShape);
  700. }
  701.  
  702. //----------------------------------------------------------------------------------------
  703. //    CDrawSelection::SelectWithRectangle
  704. //----------------------------------------------------------------------------------------
  705.  
  706. void CDrawSelection::SelectWithRectangle(Environment* ev, const FW_CMouseEvent& theMouseEvent)
  707. {
  708.     CRectShape rectShape;    // Create a rect shape on the stack
  709.     FW_PStyle trackStyle(FW_IntToFixed(1), FW_kAntPat);
  710.     FW_PInk trackInk(FW_kRGBBlack, FW_kRGBWhite, FW_kXOr);
  711.     rectShape.ChangeRenderVerb(ev, fDrawPart, kFrameOnly);
  712.     rectShape.SetFrameInk(trackInk);
  713.     rectShape.SetFrameStyle(trackStyle);
  714.     
  715.     ODFacet* facet = theMouseEvent.GetFacet(ev);
  716.     FW_CFrame *frame = FW_CFrame::ODtoFWFrame(ev, facet->GetFrame(ev));
  717.  
  718.     CShapeTracker tracker(ev, frame->GetContentView(ev), facet, &rectShape, FALSE);
  719.     if (tracker.Track(ev, theMouseEvent))
  720.     {
  721.         FW_Boolean isShift = theMouseEvent.IsExtendModifier(ev);
  722.         FW_CRect selectRect;
  723.         rectShape.GetRectGeometry(selectRect);
  724.         
  725.         FW_COrderedCollectionIterator ite(fDrawPart->GetShapeList());
  726.         for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  727.         {
  728.             if (shape->InSelectionRect(selectRect))
  729.             {
  730.                 if (shape->IsSelectedShape())
  731.                 {
  732.                     if (isShift)
  733.                     {
  734.                         DoRemove(ev, shape);
  735.                         RenderHandles(ev, shape);        // Turn Off
  736.                     }
  737.                 }
  738.                 else
  739.                 {
  740.                     DoAdd(ev, shape);
  741.                     RenderHandles(ev, shape);    // Turn on    
  742.                 }
  743.             }
  744.             else if (shape->IsSelectedShape() && !isShift)
  745.             {
  746.                 DoRemove(ev, shape);
  747.                 RenderHandles(ev, shape);        // Turn Off
  748.             }
  749.         }            
  750.         CalcCache(ev);
  751.     }
  752.     else
  753.     {
  754.         if (!theMouseEvent.IsExtendModifier(ev))
  755.             CloseSelection(ev);
  756.     }
  757. }
  758.  
  759. //----------------------------------------------------------------------------------------
  760. //    CDrawSelection::ChangeSelectionPenSize
  761. //----------------------------------------------------------------------------------------
  762.  
  763. void CDrawSelection::ChangeSelectionPenSize(Environment* ev, FW_CFixed newPenSize)
  764. {
  765.     FW_CAcquiredODShape aqUnionShape(fUpdateShape->Copy(ev));
  766.     
  767.     FW_COrderedCollectionIterator ite(fCollection);
  768.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  769.     {
  770.         shape->ChangePenSize(ev, fDrawPart, newPenSize);
  771.         this->RedrawShape(ev, shape);
  772.     }
  773.     
  774.     SelectionChanged(ev);
  775.     CalcCache(ev);
  776.  
  777.     aqUnionShape->Union(ev, fUpdateShape);
  778.     
  779.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  780.     facetClipper.Clip(ev, GetPresentation(ev), aqUnionShape);
  781. }
  782.  
  783. //----------------------------------------------------------------------------------------
  784. //    CDrawSelection::ChangeSelectionColor
  785. //----------------------------------------------------------------------------------------
  786.  
  787. void CDrawSelection::ChangeSelectionColor(Environment* ev, const FW_CColor& color, FW_Boolean changeFill)
  788. {
  789.     FW_COrderedCollectionIterator ite(fCollection);
  790.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  791.     {
  792.         if (changeFill)
  793.             shape->ChangeFillColor(ev, fDrawPart, color);
  794.         else
  795.             shape->ChangeFrameColor(ev, fDrawPart, color);
  796.         
  797.         this->RedrawShape(ev, shape);
  798.     }
  799.     
  800.     SelectionChanged(ev);
  801. }
  802.  
  803. //----------------------------------------------------------------------------------------
  804. //    CDrawSelection::ChangeSelectionPattern
  805. //----------------------------------------------------------------------------------------
  806.  
  807. void CDrawSelection::ChangeSelectionPattern(Environment* ev, const FW_PPattern& pattern, FW_Boolean changeFill)
  808. {
  809.     FW_COrderedCollectionIterator ite(fCollection);
  810.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  811.     {
  812.         if (changeFill)
  813.             shape->ChangeFillPattern(ev, fDrawPart, pattern);
  814.         else 
  815.             shape->ChangeFramePattern(ev, fDrawPart, pattern);
  816.             
  817.         this->RedrawShape(ev, shape);
  818.     }
  819.     
  820.     SelectionChanged(ev);
  821. }
  822.  
  823.  
  824. //----------------------------------------------------------------------------------------
  825. //    CDrawSelection::ChangeSelectionRenderVerb
  826. //----------------------------------------------------------------------------------------
  827.  
  828. void CDrawSelection::ChangeSelectionRenderVerb(Environment* ev, unsigned short newRenderVerb)
  829. {
  830.     FW_CAcquiredODShape aqUnionShape(fUpdateShape->Copy(ev));
  831.     
  832.     FW_COrderedCollectionIterator ite(fCollection);
  833.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  834.     {
  835.         shape->ChangeRenderVerb(ev, fDrawPart, newRenderVerb);
  836.         this->RedrawShape(ev, shape);
  837.     }
  838.     
  839.     SelectionChanged(ev);
  840.     CalcCache(ev);
  841.     
  842.     aqUnionShape->Union(ev, fUpdateShape);
  843.     
  844.     CDrawFacetClipper facetClipper(ev, fDrawPart);
  845.     facetClipper.Clip(ev, GetPresentation(ev), aqUnionShape);
  846. }
  847.  
  848. //----------------------------------------------------------------------------------------
  849. //    CDrawSelection::RedrawShape
  850. //----------------------------------------------------------------------------------------
  851.  
  852. void CDrawSelection::RedrawShape(Environment* ev, CBaseShape *shape)
  853. {
  854.     FW_CAcquiredODShape aqInvalidShape = ::FW_NewODShape(ev);
  855.     shape->GetUpdateBox(ev, aqInvalidShape);        
  856.     GetPresentation(ev)->Invalidate(ev, aqInvalidShape);
  857. }
  858.  
  859. //----------------------------------------------------------------------------------------
  860. //    CDrawSelection::SetFrozen
  861. //----------------------------------------------------------------------------------------
  862.  
  863. void CDrawSelection::SetFrozen(Environment* ev, FW_Boolean state)
  864. {
  865.     FW_COrderedCollectionIterator ite(fCollection);
  866.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  867.     {
  868.         if (shape->SetFrozen(state))
  869.         {
  870.             state ? fFrozenCount++ : fFrozenCount--;
  871.         }
  872.     }
  873. }
  874.  
  875. //----------------------------------------------------------------------------------------
  876. //    CDrawSelection::NewPromise
  877. //----------------------------------------------------------------------------------------
  878.  
  879. FW_CPromise* CDrawSelection::NewPromise(Environment* ev, ODUpdateID updateID, FW_EStorageKinds storageKind, FW_CFrame* scopeFrame, ODCloneKind cloneKind)
  880. {
  881. FW_UNUSED(cloneKind);
  882.  
  883.     if (storageKind == FW_kLinkStorage)
  884.         return NULL;
  885.  
  886.     return new CDrawPromise(ev, updateID, storageKind, fDrawPart, scopeFrame, this);
  887. }
  888.  
  889. //----------------------------------------------------------------------------------------
  890. //    CDrawSelection::SelectionChanged
  891. //----------------------------------------------------------------------------------------
  892.  
  893. void CDrawSelection::SelectionChanged(Environment* ev)
  894. {
  895.     FW_CPrivOrderedCollection temp;
  896.     
  897.     FW_COrderedCollectionIterator ite(fCollection);
  898.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  899.     {
  900.         if (shape->IsPublished())
  901.         {
  902.             FW_Boolean add = TRUE;
  903.             FW_COrderedCollectionIterator tempIte(&temp);
  904.             for (CDrawPublishLink* link = (CDrawPublishLink*)tempIte.First(); tempIte.IsNotComplete(); link = (CDrawPublishLink*)tempIte.Next())
  905.             {
  906.                 if (shape->GetPublishLink() == link)
  907.                 {
  908.                     add = FALSE;
  909.                     break;
  910.                 }
  911.             }
  912.             
  913.             if (add)
  914.                 temp.AddLast(shape->GetPublishLink());
  915.         }
  916.     }
  917.     
  918.     FW_COrderedCollectionIterator tempIte(&temp);
  919.     for (CDrawPublishLink* link = (CDrawPublishLink*)tempIte.First(); tempIte.IsNotComplete(); link = (CDrawPublishLink*)tempIte.Next())
  920.     {
  921.         link->ContentUpdated(ev, fDrawPart->GetSession(ev)->UniqueUpdateID(ev));
  922.     }
  923.     
  924.     temp.RemoveAll();
  925. }
  926.  
  927. //----------------------------------------------------------------------------------------
  928. //    CDrawSelection::IsSelectionPublishable
  929. //----------------------------------------------------------------------------------------
  930. //    We don't allow linking if one of the shape is already published
  931.  
  932. FW_Boolean CDrawSelection::IsSelectionPublishable(Environment* ev)
  933. {
  934.     FW_Boolean result = TRUE;
  935.     
  936.     FW_COrderedCollectionIterator ite(fCollection);
  937.     for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  938.     {
  939.         if (shape->IsPublished())
  940.         {
  941.             result = FALSE;
  942.             break;
  943.         }
  944.     }
  945.     
  946.     return result;
  947. }
  948.  
  949. //----------------------------------------------------------------------------------------
  950. //    CDrawSelection::CanSubscribe
  951. //----------------------------------------------------------------------------------------
  952. FW_Boolean CDrawSelection::CanSubscribe(Environment* ev, ODPasteAsMergeSetting& setting)
  953. {
  954.     FW_Boolean result = TRUE;
  955.     
  956.     if (fAllowSubscribe)
  957.     {
  958.         setting = kODPasteAsMerge;    // default is no embedding
  959.  
  960.         FW_COrderedCollectionIterator ite(fCollection);
  961.         for (CBaseShape *shape = (CBaseShape*)ite.First(); ite.IsNotComplete(); shape = (CBaseShape*)ite.Next())
  962.         {
  963.             if (shape->IsSubscribed())
  964.             {
  965.                 result = FALSE;
  966.                 break;
  967.             }
  968.         }
  969.     }
  970.     
  971.     return result;
  972. }
  973.  
  974. //----------------------------------------------------------------------------------------
  975. //    CDrawSelection::IsMouseInDraggableItem
  976. //----------------------------------------------------------------------------------------
  977.  
  978. FW_Boolean CDrawSelection::IsMouseInDraggableItem(Environment* ev, FW_CFrame* frame, 
  979.                         const FW_CMouseEvent& theMouseEvent, FW_Boolean inBackground)
  980. {    
  981.     fClickedHandle = 0;
  982.     fAnchorShape = NULL;
  983.  
  984.     if (fDrawPart->GetTool() != kSelectTool)
  985.         return FALSE;
  986.         
  987.     FW_CViewContext vc(ev, frame->GetContentView(ev), theMouseEvent.GetFacet(ev));
  988.  
  989.     // ----- Look first for a handle -----
  990.     FW_CPoint where = theMouseEvent.GetMousePosition(ev, FW_CMouseEvent::kFrame);
  991.     frame->GetContentView(ev)->FrameToViewContent(ev, where);
  992.  
  993.     CBaseShape* clickedShape = WhichHandle(ev, vc, where, fClickedHandle);
  994.     if (clickedShape != NULL)
  995.     {
  996.         fAnchorShape = clickedShape;
  997.         return FALSE;            // if in a handle we won't drag
  998.     }
  999.     
  1000.     // ----- then look for a selected shape -----
  1001.     fAnchorShape = fDrawPart->WhichShape(ev, vc, theMouseEvent, TRUE);
  1002.         
  1003.     return fAnchorShape != NULL;
  1004. }
  1005.  
  1006. //----------------------------------------------------------------------------------------
  1007. // CDrawSelection::UpdateSelectionOnMouseDown
  1008. //----------------------------------------------------------------------------------------
  1009.  
  1010. void CDrawSelection::UpdateSelectionOnMouseDown(Environment* ev, 
  1011.                                             const FW_CMouseEvent& mouseEvent,
  1012.                                             ODFacet* embeddedFacet,
  1013.                                             FW_Boolean inEmbeddedFrameBorder,
  1014.                                             FW_Boolean inBackground)
  1015. {
  1016.     if (inEmbeddedFrameBorder)
  1017.     {
  1018.         fClickedHandle = 0;
  1019.         fAnchorShape = NULL;
  1020.  
  1021.         CProxyShape *theShape = (CProxyShape*)fDrawPart->GetProxy(ev, embeddedFacet->GetFrame(ev));
  1022.             
  1023. //        [HLX] I have to remove this assert for now. See FW_CFrame::FocusStateChanged            
  1024. //        FW_ASSERT(!theShape->IsSelectedShape());
  1025.         CloseSelection(ev);    // Should already be closed
  1026.         AddToSelection(ev, theShape, TRUE);
  1027.         
  1028.         fAnchorShape = theShape;
  1029.     }
  1030.     else if (fDrawPart->GetTool() == kSelectTool)
  1031.     {
  1032.         FW_CFrame *frame = FW_CFrame::ODtoFWFrame(ev, mouseEvent.GetFacet(ev)->GetFrame(ev));
  1033.         FW_CViewContext vc(ev, frame->GetContentView(ev), mouseEvent.GetFacet(ev));
  1034.         
  1035.         CBaseShape* clickedShape = fDrawPart->WhichShape(ev, vc, mouseEvent, FALSE);
  1036.         if (clickedShape)
  1037.         {
  1038.             if (mouseEvent.IsExtendModifier(ev))
  1039.             {
  1040.                 if (clickedShape->IsSelectedShape())
  1041.                     RemoveFromSelection(ev, clickedShape, TRUE);
  1042.                 else
  1043.                     AddToSelection(ev, clickedShape, TRUE);
  1044.             }
  1045.             else if (!clickedShape->IsSelectedShape())
  1046.             {
  1047.                 CloseSelection(ev);
  1048.                 AddToSelection(ev, clickedShape, TRUE);
  1049.             }    
  1050.         }
  1051.     }
  1052. }
  1053.  
  1054. //----------------------------------------------------------------------------------------
  1055. //    CDrawSelection::DeleteSelection
  1056. //----------------------------------------------------------------------------------------
  1057. void CDrawSelection::DeleteSelection(Environment* ev)
  1058. {
  1059.     CBaseShape *shape;
  1060.     while (fCollection->Count() != 0)
  1061.     {
  1062.         shape = (CBaseShape*)fCollection->First();
  1063.         DoRemove(ev, shape);                // Remove from Selection list
  1064.         fDrawPart->DeleteShape(ev, shape);    // Remove from shape list
  1065.         delete shape;
  1066.     }
  1067. }
  1068.  
  1069. //----------------------------------------------------------------------------------------
  1070. //    CDrawSelection::IsOKtoEdit
  1071. //----------------------------------------------------------------------------------------
  1072.  
  1073. FW_Boolean CDrawSelection::IsOKtoEdit(Environment* ev)
  1074. {
  1075.     if (fDrawPart->IsReadOnly(ev))    // can't edit read only part
  1076.         return FALSE;
  1077.  
  1078.     // check whether any of the selected shapes is in a link destination
  1079.     FW_COrderedCollectionIterator iter(fCollection);
  1080.     for (CBaseShape *shape = (CBaseShape*)iter.First(); iter.IsNotComplete(); shape = (CBaseShape*)iter.Next())
  1081.     {
  1082.         if (shape->IsSubscribed())
  1083.             return FALSE;            // can't edit a subscribed shape
  1084.     }
  1085.  
  1086.     return TRUE;
  1087. }
  1088.  
  1089. //----------------------------------------------------------------------------------------
  1090. //    CDrawSelection::GetSelectionPenSize
  1091. //----------------------------------------------------------------------------------------
  1092.  
  1093. FW_Boolean CDrawSelection::GetSelectionPenSize(Environment* ev, FW_CFixed& penSize)
  1094. {
  1095.     FW_COrderedCollectionIterator iter(fCollection);
  1096.     FW_Boolean first = TRUE;
  1097.     for (CBaseShape *shape = (CBaseShape*)iter.First(); iter.IsNotComplete(); shape = (CBaseShape*)iter.Next())
  1098.     {
  1099.         if (first)
  1100.         {
  1101.             penSize = shape->GetPenSize();
  1102.             first = FALSE;
  1103.         }
  1104.         else
  1105.         {
  1106.             if (shape->GetPenSize() != penSize)
  1107.                 return FALSE;
  1108.         }
  1109.     }
  1110.     
  1111.     return TRUE;
  1112. }
  1113.